package work.utils;

import java.util.ArrayList;
import java.util.List;

//身份证号格式化
public class IdCardFormat {
    public static String format(String s) {

        if (s == null) {
            return null;
        }

        //如果满足直接返回
        if (s.matches("^[1-9]\\d{5}(18|19|20|(3\\d))\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$")) {
            return s;
        }

        char[] chars = s.toCharArray();

        StringBuilder builder = new StringBuilder();

        for (char c : chars) {
            if (c > 47 && c < 58) {
                builder.append(c);
            } else if (c == 120 || c == 88) {
                builder.append('X');
            }
        }

        return builder.toString();
    }

}

